part of 'api.dart';

// ignore: library_private_types_in_public_api
extension ApiAnime on _Api {
  // 获取新番
  Future<EpisodeModel?> fetchEpisode(String? recordTime) async {
    try {
      final resp = await httpInstance.get(
        url: '/episode/getEpisode',
        queryMap: {'recordTime': recordTime},
        complete: EpisodeModel.fromJson,
      );

      return resp;
    } catch (e) {
      return null;
    }
  }

  // 获取新番相关作品
  Future<List<VideoBaseModel>?> fetchEpisodeRelateVideo(int episodeId) async {
    try {
      final resp = await httpInstance.get(
        url: '/episode/getRelateVideo',
        queryMap: {'episodeId': episodeId},
        complete: VideoBaseModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }

  // 评论点赞
  Future<bool?> episodeCommentLike(int commentId) async {
    try {
      await httpInstance.post(url: '/episode/comment/saveLike', body: {
        'commentId': commentId,
      });
      return true;
    } catch (e) {
      return null;
    }
  }

  // 取消评论点赞
  Future<bool?> episodeCommentUnLike(int commentId) async {
    try {
      await httpInstance.post(
        url: '/episode/comment/unLike',
        body: {'commentId': commentId},
      );
      return true;
    } catch (e) {
      return null;
    }
  }

  // toogle 点赞
  Future<bool?> toogleEpisodeCommentLike(int commentId, {required bool? like}) {
    if (like == true) {
      return episodeCommentUnLike(commentId);
    } else {
      return episodeCommentLike(commentId);
    }
  }

  // 获取评论列表
  Future<List<CommentModel>?> fetchEpisodeCommentList({
    int? episodeId,
    required int page,
    required int pageSize,
    int? parentId,
  }) async {
    try {
      final resp = await httpInstance.get(
        url: '/episode/commentList',
        queryMap: {
          'episodeId': episodeId,
          'page': page,
          'pageSize': pageSize,
          'parentId': parentId,
        },
        complete: CommentModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }

  Future<bool?> saveEpisodeComment({
    required String content,
    required int episodeId,
    String? img,
    int? parentId,
    int? topId,
  }) async {
    try {
      await httpInstance.post(url: '/episode/saveComment', body: {
        'content': content,
        'episodeId': episodeId,
        'img': img,
        'parentId': parentId,
        'topId': topId,
      });
      return true;
    } catch (e) {
      return null;
    }
  }
}
